package com.jeesite.modules.customer.web;


import com.jeesite.common.utils.OrgUtil;
import com.jeesite.modules.customer.entity.constans.CustomerConstant;
import com.jeesite.modules.customer.entity.qo.CustomerQO;
import com.jeesite.modules.customer.entity.vo.CustomerVO;
import com.jeesite.modules.customerclass.entity.CustomerClass;
import com.jeesite.modules.customerclass.service.CustomerClassService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.customer.entity.Customer;
import com.jeesite.modules.customer.service.CustomerService;

/**
 * 客户档案Controller
 * @author 码脑洞
 * @version 2025-04-21
 */
@Controller
@RequestMapping(value = "${adminPath}/customer/customer")
public class CustomerController extends BaseController {

	@Autowired
	private CustomerService customerService;
	@Autowired
	private CustomerClassService customerClassService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Customer get(Long autoId, boolean isNewRecord) {
		return customerService.get(autoId, isNewRecord);
	}


	@RequiresPermissions("customer:customer:view")
	@RequestMapping(value = "index")
	public String index(CustomerClass customerClass, Model model) {
		model.addAttribute("customerClass", customerClass);
		model.addAttribute("ctrlPermi", Global.getConfig("user.adminCtrlPermi", "2"));
		return "modules/customer/customerIndex";
	}


	/**
	 * 查询列表
	 */
	@RequiresPermissions("customer:customer:view")
	@RequestMapping(value = {"list", ""})
	public String list(Customer customer, Model model) {
		model.addAttribute("customer", customer);
		return "modules/customer/customerList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("customer:customer:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<CustomerVO> listData(CustomerQO customerQO, HttpServletRequest request, HttpServletResponse response) {
		customerQO.setPage(new Page<>(request, response));
		Page<CustomerVO> page = customerService.findCustomerPage(customerQO);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("customer:customer:view")
	@RequestMapping(value = "form")
	public String form(Customer customer, Model model) {
		model.addAttribute("customer", customer);
		if (OrgUtil.isValid()){
			customer.setOrgId(OrgUtil.getOrgId());
			customer.setOrgCode(OrgUtil.getOrgCode());
			customer.setOrgName(OrgUtil.getOrgName());
			customer.setSourceType(CustomerConstant.SOURCE_TYPE_MANUAL);
		}
		model.addAttribute("customerClassList", customerClassService.selectCustomerClassList());
		return "modules/customer/customerForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("customer:customer:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Customer customer) {
		customerService.save(customer);
		return renderResult(Global.TRUE, text("保存客户档案成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("customer:customer:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Customer customer) {
		customer.setStatus(Customer.STATUS_DISABLE);
		customerService.updateStatus(customer);
		return renderResult(Global.TRUE, text("停用客户档案成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("customer:customer:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Customer customer) {
		customer.setStatus(Customer.STATUS_NORMAL);
		customerService.updateStatus(customer);
		return renderResult(Global.TRUE, text("启用客户档案成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("customer:customer:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Customer customer) {
		customerService.delete(customer);
		return renderResult(Global.TRUE, text("删除客户档案成功！"));
	}
	
}